
ItemEvents.rightClicked('eou:anchor_staff', event => {
    var { level, player, hand, item } = event
    var { x, y, z, yaw, pitch } = player
    pitch = Math.max(-88, Math.min(88, pitch))
    if (true) {
        var lookPose = EntityHealper.getEntityLookPositionOnblock(player, 8)
        // level.spawnParticles("minecraft:dripping_dripstone_water", true, lookPose.x(), lookPose.y(), lookPose.z(), 0, 0, 0, 1, 0)
        var ArmorStandProj = new $ArmorStand(player.level, player.x, player.y, player.z)
        var { nbt } = ArmorStandProj
        ArmorStandProj.addTag("anchor_staff_proj")
        nbt.putBoolean("Marker", true)
        ArmorStandProj.setNbt(nbt)
        ArmorStandProj.setInvisible(true)
        ArmorStandProj.forgePersistentData.put("aimPos", { x: lookPose.x(), y: lookPose.y(), z: lookPose.z() })
        ArmorStandProj.forgePersistentData.put("startPos", { x: x, y: y, z: z })
        var horizontalDistance = Math.sqrt(((lookPose.x() - x) ** 2) + ((lookPose.z() - z) ** 2))
        ArmorStandProj.forgePersistentData.putFloat("horizontalDistance", horizontalDistance.toFixed(2))
        ArmorStandProj.forgePersistentData.putFloat("n", 0)
        ArmorStandProj.forgePersistentData.putShort("explodeWait", 5)
        ArmorStandProj.forgePersistentData.putUUID("sourcePlayer", player.uuid)
        ArmorStandProj.setHeadArmorItem(new $ItemStack("eou:model_item").withNBT({ CustomModelData: 2 }))
        ArmorStandProj.setHeadPose(new $Rotations(-(90 - pitch) / 2, 0, 0))
        ArmorStandProj.setRotation(yaw, pitch)
        level.addFreshEntity(ArmorStandProj)
    }
})

severEntityUpdater.EntityTick(entity => {
    if (entity instanceof $ArmorStand && entity.getTags().contains("anchor_staff_proj")) {
        entity.level.spawnParticles("immersiveengineering:sparks", true, entity.xOld, entity.yOld + 1.6, entity.zOld, 0, 0, 0, 1, 0)
        var speed = 0.1

        var { forgePersistentData, yaw } = entity
        var attacker = playerHelper.getPlayerByUUID(forgePersistentData.getUUID("sourcePlayer"))
        var { x: aimPosx, y: aimPosy, z: aimPosz } = forgePersistentData.get("aimPos")
        var { x: startPosx, y: startPosy, z: startPosz } = forgePersistentData.get("startPos")
        var relativeHeight = aimPosy - startPosy
        var n = forgePersistentData.getFloat("n")
        var horizontalDistance = entity.forgePersistentData.getFloat("horizontalDistance")
        var topx = horizontalDistance / 2
        var topy = aimPosy > startPosy ? relativeHeight + topx * Math.sqrt(3) : topx * Math.sqrt(3)



        n = Math.min(n + 0.5 / (horizontalDistance + 1), 1)
        entity.forgePersistentData.putFloat("n", n)
        var p1 = mth.Vec3_lerp(new Vec3(0, 0, 0), new Vec3(topx, topy, 0), n)
        var p2 = mth.Vec3_lerp(new Vec3(topx, topy, 0), new Vec3(horizontalDistance, relativeHeight, 0), n)
        var p3 = mth.Vec3_lerp(p1, p2, n)
        var k = (p2.y() - p1.y()) / (p2.x() - p1.x())

        entity.setHeadPose(new $Rotations(Math.max(-88, Math.min(88, -Math.atan((p2.y() - p1.y()) / (p2.x() - p1.x())) * 180 / PI)), 0, 0))
        entity.setPosition(startPosx - p3.x() * Math.sin(entity.yaw * PI / 180), startPosy + p3.y() - 1.6, startPosz + p3.x() * Math.cos(entity.yaw * PI / 180))


        if (n == 1) {
            var wait = forgePersistentData.getShort("explodeWait")
            wait = Math.max(0, wait - 1)
            entity.forgePersistentData.putShort("explodeWait", wait)
            if (wait == 0) {
                entity.level.createExplosion(aimPosx, aimPosy, aimPosz).destroysTerrain(false).exploder(attacker).explode()
                entity.kill()
            }
        }
    }
})